package com.xc.freeapp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xc.freeapp.common.PageInfo;
import com.xc.freeapp.dao.DeptInfoDao;
import com.xc.freeapp.dao.UserInfoDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.DeptInfo;
import com.xc.freeapp.entity.UserInfo;
import com.xc.freeapp.service.UserService;

/**
 * 数据处理具体实现
 * 
 * @author ZhouXinjie
 * 
 */

@Service
public class UserServiceImpl implements UserService {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private DeptInfoDao deptDao;

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo login(String userName, String password) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("验证用户");
		return userInfoDao.login(userName, password);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<UserInfo> queryUsers(PageInfo<UserInfo> pageInfo, Map<String, String> map) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		if (pageInfo != null) {
			log.info("设置分页");
			pageInfo.setTotalRecords(userInfoDao.getTotalRows(map));
		}
		log.info("获取记录");
		return userInfoDao.queryUsers(new RowBounds(pageInfo.getFromRecord(), pageInfo.getPageSize()), map);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<UserInfo> queryForJson() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("获取记录");
		return userInfoDao.queryUsers();
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo getUser(String userId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return userInfoDao.getUser(userId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void saveUser(UserInfo userInfo) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		userInfoDao.saveUser(userInfo);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void updateUser(UserInfo userInfo) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		userInfoDao.updateUser(userInfo);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deleteUser(String[] idItem) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		List<DeptInfo> depts = null;
		for (String userId : idItem) {
			log.info("检测是否是部门经理，如果是，这设置所在部门部门经理为空");
			depts = deptDao.getDeptByDeptManager(userId);
			if (depts != null && depts.size() > 0) {
				for (DeptInfo dept : depts) {
					dept.setDeptManager("");
					deptDao.updateDept(dept);
				}
			}
		}
		userInfoDao.deleteUser(idItem);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int checkUserName(String userName) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return userInfoDao.checkUserName(userName);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<UserInfo> getAllUsers() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return userInfoDao.getAllUsers();
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public UserInfo downloadFile(String userId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return userInfoDao.downloadFile(userId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int deleteFile(String userId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return userInfoDao.deleteFile(userId);
	}
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public String updatePWD(String userId, String oldpwd, String newpwd) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		try {
			String password = userInfoDao.getUserPassword(userId);
			if (!oldpwd.equals(password)) {
				return "error";
			} else {
				userInfoDao.updatePWD(userId, newpwd);
				return "success";
			}
		} catch (Exception e) {
			return "fail";
		}
	}


}
